package com.slmxz.tms.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author：albert
 * @Date：2024/9/21 8:28
 */
public class LoggerUtil {
    private static final Map<String,Logger> logMap = new HashMap<>();
    private static final boolean infoEnable = true;
    private static final boolean errorEnable = true;
    public static <T> void info(T t,String content,Object ... args){
        if(!infoEnable){
            //禁用日志打印
            return;
        }
        Logger log = getLogger(t);
        if(args == null || args.length == 0){
            log.info(content);
            return;
        }
        log.info(content,args);
    }
    public static <T> void error(T t,String content){
       error(t,content, "");
    }
    public static <T> void error(T t,String content,Object ... args){
        if(!errorEnable){
            //禁用日志打印
            return;
        }
        Logger log = getLogger(t);
        if(args == null || args.length == 0){
            log.error(content);
            return;
        }
        log.error(content,args);
    }

    /**
     * 获取日志记录对象
     * @return
     */
    private static <T> Logger getLogger(T t){
        String cacheKay = t.getClass().getName();
        Logger log = logMap.get(cacheKay);
        if(log == null){
            log = LoggerFactory.getLogger(t.getClass());
            logMap.put(cacheKay,log);
        }
        return log;
    }
}
